package com.springboot.test.springbootcrud.component;

import java.util.Map;
import org.springframework.boot.autoconfigure.web.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/12/28 11:00
 * <p>
 * 给容器中加入我们自己定义的ErrorAttribute
 */
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {

    /**
     * 返回值map就是页面和json能获取的所有字段
     *
     * @param requestAttributes
     * @param includeStackTrace
     * @return
     */
    @Override
    public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
        Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
        errorAttributes.put("company", "atguigu");

        //我们的异常处理器携带的数据
        Map<String, Object> ext = (Map<String, Object>) requestAttributes.getAttribute("ext", 0);
        errorAttributes.put("ext", ext);
        return errorAttributes;
    }
}
